<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="adoptOrders" highlight-current-row v-loading="listLoading"
				  @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="orderSn" label="编号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="digest" label="摘要" width="180" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: deeppink;" v-if="scope.row.state === 0">待支付</span>
					<span style="color: aquamarine" v-else-if="scope.row.state === 1">待发货</span>
					<span style="color: coral" v-else-if="scope.row.state === 2">待确认</span>
					<span style="color: green;" v-else-if="scope.row.state === 3">已完成</span>
					<span style="color: gray;" v-else-if="scope.row.state === -1">取消</span>
					<span style="color: dimgray;"v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="price" label="价格" width="120" sortable>
			</el-table-column>
			<el-table-column prop="lastPayTime" label="最后支付时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="lastConfirmTime" label="最后确认时间" width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="orderInfo(scope.$index, scope.row)">详情</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   :current-page="currentPage"
						   style="float:right;">
			</el-pagination>
		</el-col>



		<el-dialog title="订单详情" :visible.sync="adoptOrderInfoVisible" :close-on-click-modal="false">
			<el-form :model="adoptOrderInfo" label-width="120px" ref="adoptOrderInfo">
				<el-form-item label="摘要" prop="digest">
					<el-input readonly="readonly" v-model="adoptOrderInfo.digest" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="订单号" prop="orderSn">
					<el-input readonly="readonly" v-model="adoptOrderInfo.orderSn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="price">
					<el-input readonly="readonly" v-model="adoptOrderInfo.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="state">
					<template scope="scope">
						<span style="color: green" v-if="adoptOrderInfo.state==0">待支付</span>
						<span style="color: red" v-else-if="adoptOrderInfo.state==1">已完成</span>
						<span style="color: gray" v-else>未知</span>
					</template>
				</el-form-item>
				<el-form-item label="最后确认时间" prop="lastConfirmTime">
					<el-input v-model="adoptOrderInfo.lastConfirmTime" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="宠物名称" prop="manager">
					<el-input readonly="readonly" v-model="adoptOrderInfo.pet.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="领养人" prop="parent">
					<el-input readonly="readonly" v-model="adoptOrderInfo.user.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="门店" prop="parent">
					<el-input readonly="readonly" v-model="adoptOrderInfo.shop.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="adoptOrderInfoVisible = false">取消</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''//关键字
                },
                adoptOrders: [],
                total: 0,
                currentPage: 1,
                pageSize:5,
                listLoading: false,
                sels: [],//列表选中所有行
                //详情
                adoptOrderInfoVisible:false,
                adoptOrderInfo: {
                    digest: '',
                    state: '',
                    price: 0,
                    orderSn: null,
                    paySn: null,
                    lastPayTime:'',
                    lastConfirmTime: '',
                    pet: {},
                    user: {},
                    shop: {}
                }
            }
        },
        methods: {
            orderInfo(index,row){
                let orderId = row.id;
                this.$http.get("/adoptOrder/"+orderId)
                    .then(result=>{
                        this.adoptOrderInfo = result.data;// Pet
                        this.adoptOrderInfoVisible = true;//显示编辑窗口
                    })
                    .catch(result=>{

                    })
            },
            search(){
                this.currentPage = 1;//高级查询必须定位到第一页
                this.getAdoptOrders();
            },
            handleCurrentChange(val) {
                this.currentPage = val;//3
                this.getAdoptOrders();
            },
            //获取用户列表
            getAdoptOrders() {
                //AdoptOrderQuery
                let para = {
                    currentPage: this.currentPage,//3
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                //NProgress.start();
                //使用axios发送请求，从pethome拿数据 http://localhost:8080/dept/list
                this.$http.post("/adoptOrder/admin",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        console.log(result);
                        result = result.data;//PageList
                        this.adoptOrders = result.rows;
                        this.total = result.totals;//总条数
                    })
                    .catch(result=>{
                        console.log(result);
                        this.$message({
                            message: '系统繁忙，稍后重试！！！',
                            type: 'error'
                        });
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启列表加载忙等
                    this.$http.delete("/dept/"+row.id)
                        .then(result=>{//后端执行完成了
                            this.listLoading = false;//关闭列表加载忙等
                            result = result.data;//  AjaxResult
                            //{success:true,message:操作成功}
                            //{success:false,message:操作失败}
                            //1.提示
                            if(result.success){
                                this.$message({
                                    message: '操作成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示后端的业务错误
                                    type: 'error'
                                });
                            }
                            //2.刷新页面  是否需要定位到第一
                            this.currentPage = 1;
                            this.getAdoptOrders();
                        })
                        .catch(result=>{
                            this.listLoading = false;//关闭列表加载忙等
                            console.log(result);
                            //1.提示
                            this.$message({
                                message: '系统繁忙，稍后重试！！！',
                                type: 'error'
                            });
                            //2.刷新页面 要   是否需要定位到第一页
                            this.currentPage = 1;
                            this.getAdoptOrders();
                        });
                }).catch(() => {

                });
            },
            selsChange: function (sels) {//当前列表页选中的所有行的数据
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);//[1,2,3]  .toString()
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/dept",ids)
                        .then(result=>{//后端执行完成了
                            this.listLoading = false;//关闭列表加载忙等
                            result = result.data;//  AjaxResult
                            //{success:true,message:操作成功}
                            //{success:false,message:操作失败}
                            //1.提示
                            if(result.success){
                                this.$message({
                                    message: '操作成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示后端的业务错误
                                    type: 'error'
                                });
                            }
                            //2.刷新页面  是否需要定位到第一
                            this.currentPage = 1;
                            this.getAdoptOrders();
                        })
                        .catch(result=>{
                            this.listLoading = false;//关闭列表加载忙等
                            console.log(result);
                            //1.提示
                            this.$message({
                                message: '系统繁忙，稍后重试！！！',
                                type: 'error'
                            });
                            //2.刷新页面 要   是否需要定位到第一页
                            this.currentPage = 1;
                            this.getAdoptOrders();
                        });
                }).catch(() => {

                });
            }
        },
        mounted() {
            //页面加载完毕，调用pethome拿出数据
            this.getAdoptOrders();
        }
    }

</script>

<style scoped>

</style>